package leetcode.templates.maths;


//1. (a + b) % p = (a % p + b % p) % p
//2. (a - b) % p = (a % p - b % p) % p
//3. (a * b) % p = (a % p * b % p) % p
//4. (a^b) % p = ((a % p)^b) % p
public class MaxMaths {

	// 区间相交
	public boolean merge(int L, int R, int l, int r) {
		int left = Math.max(L, l);
		int right = Math.min(R, r);
		if (left <= right) {
			return true;
		}
		return false;
	}

	int mod = (int) 1e9 + 7;

	/**
	 * 最大公约数
	 */
	public int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}

	// 最小公倍数
	public int lcm(int a, int b) {
		return a * b / gcd(a, b);
	}

	// 点共线
	public boolean checkStraightLine(int[][] coordinates) {
		int len = coordinates.length;
		if (len == 2) {
			return true;
		}
		int a = coordinates[0][0], b = coordinates[0][1];
		for (int i = 2; i < len; i++) {
			int[] cur = coordinates[i];
			int[] last = coordinates[i - 1];
			if ((cur[1] - b) * (last[0] - a) != (cur[0] - a) * (last[1] - b)) {
				return false;
			}
		}
		return true;
	}

	// 阶乘
	public long factorial(int num) {
		long ans = 1;
		for (int i = 1; i <= num; i++) {
			ans *= i;
			ans %= mod;
		}
		return ans;
	}

	// 质数
	public boolean isPrime(int num) {
		if (num == 2) {
			return true;
		}
		if (num == 1 || num % 2 == 0) {
			return false;
		}
		for (int i = 3; i * i <= num; i += 2) {
			if (num % i == 0 || i * i == num) {
				return false;
			}
		}
		return true;
	}

	// 组合打表，有必要就升级long
	static long[] muls;

	static {
		muls = new long[7];
		muls[0] = 1;
		long mul = 1;
		for (int i = 1; i < 7; i++) {
			mul *= i;
			muls[i] = mul;
		}
	}

	public long c(int n, int m) {
		return muls[n] / (muls[m] * muls[n - m]);
	}
}
